/**
 * Created by fupeng on 17/3/9.
 */
var cinemaCollectionDao =  require('../dao/CinemaCollectionDao');
var memberDao = require('../dao/MemberDao');
 module.exports = {
    updateCinemaCollection:function (req,callback) {
        var cinema_code = 0;
        if(req.body.cinema_code){
            cinema_code = req.body.cinema_code;
        }
        var open_id = req.signedCookies.openid||req.signedCookies.open_id;
        if(open_id){
            memberDao.selectByopenid(open_id,function(data){
                if(data.rows[0]){
                    var member_id = data.rows[0].member_id;
                    var param = [member_id,cinema_code];
                    cinemaCollectionDao.selectCinemaCollectionByMemberidAndCinemaCode(param,function(data){
                        if(data.isSuccess){
                            if(data.rows[0]){
                                cinemaCollectionDao.deleteCinemaCollectionByMemberidAndCinemaCode(param,function (data) {
                                    if(data.isSuccess){
                                        callback({
                                            status: 'success',
                                            message:'取消收藏成功'
                                        });
                                    }else{
                                        callback({
                                            status: 'error',
                                            message:'取消收藏失败'
                                        });
                                    }
                                });
                            }else{
                                cinemaCollectionDao.addCinemaCollectionByMemberidAndCinemaCode(param,function (data) {
                                    if(data.isSuccess){
                                        callback({
                                            status: 'success',
                                            message:'收藏成功'
                                        });
                                    }else{
                                        callback({
                                            status: 'error',
                                            message:'收藏失败'
                                        });
                                    }
                                });
                            }

                        }
                    });
                }else{
                    callback({
                        status: 'error',
                        message:'请先注册会员'
                    });
                }
            })
        }
    },
    getCinemaInfoLimitModel:function(req, callback){
        if (/^\d+$/.test(req.body.lastIndex)) {
            if(req.body.open_id) {
                memberDao.selectByopenid(req.body.open_id, function (data) {
                    if (data.rows[0]) {
                        var member_id = data.rows[0].member_id;
                        var lastIndex = 0;
                        if(/^\d+$/.test(req.body.lastIndex)){
                            lastIndex = parseInt(req.body.lastIndex)
                        };
                        cinemaCollectionDao.getCinema({
                            citycode: req.body.citycode,
                            cinema_name: req.body.cinema_name,
                            lat: req.body.lat,
                            long: req.body.long,
                            lastIndex: lastIndex,
                            member_id: member_id
                        }, function (data) {
                            var cinemaInfo = [];
                            if (data.isSuccess) {
                                data.rows.forEach(function (val) {
                                    cinemaInfo.push({
                                        cinema_code: val.cinema_code,
                                        cinemaName: val.cinema_name,
                                        cinemaAddress: val.cinema_site,
                                        cinema_picture: val.cinema_picture,
                                        distance: val.distance + '',
                                        score: val.score
                                    });

                                });
                                callback({
                                    message: 'success',
                                    status: 'success',
                                    data: cinemaInfo
                                });
                            } else {
                                callback({
                                    message: 'error',
                                    status: 'error',
                                    data: ''
                                });
                            }
                        })
                    }
                })
            }else{
                callback({
                    message: 'error',
                    status: 'error',
                    data: ''
                });
            }
        }else{
            callback({
                message: 'error',
                status: 'error',
                data: ''
            });
        }
    }
}
